package imvu.rbox.object
{
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import imvu.rbox.event.BoxEvent;
	import imvu.rbox.global.GStatic;
	import imvu.rbox.struct.CGamePlayerInfo;

	public class ScoreObject extends Sprite
	{
		public var mcHot:Sprite;			//	热响应区块
		public var bgSprite:Sprite;			//	背景面板
		public var btnClose:SimpleButton;	//	关闭按钮
		
		public var score1:ScoreLines;		//	第 1 名成绩
		public var score2:ScoreLines;		//	第 2 名成绩
		public var score3:ScoreLines;		//	第 3 名成绩
		public var score4:ScoreLines;		//	第 4 名成绩
		public var score5:ScoreLines;		//	第 5 名成绩
		public var score6:ScoreLines;		//	第 6 名成绩		 
		
		public function ScoreObject()
		{
			this.cacheAsBitmap = true;
			
			for(var i:int = 1; i <= 6; i++)
			{
				var sInfo:ScoreLines = this["score" + i];
				sInfo.mouseChildren = sInfo.mouseEnabled = false;
			}
			
			this.btnClose.addEventListener(MouseEvent.CLICK, onCloseThis);			
		}
		
		public function Show():void
		{
			this.mcHot.graphics.clear();
			this.mcHot.graphics.beginFill(0xFFFFFF, 0);
			this.mcHot.graphics.drawRect(-3000, -3000, 6000, 6000);
			this.mcHot.graphics.endFill();
			
			this.visible = true;
		}
		
		public function Hidden():void
		{
			this.mcHot.graphics.clear();
			this.visible = false;
		}
		
		public function ShowScore(array:Array, numTime:Number):void
		{	
			var i:int, j:int;
			var obj:Object;
			var pObj:CGamePlayerInfo;

			this.score1.InitBase();	
			this.score2.InitBase();	
			this.score3.InitBase();	
			this.score4.InitBase();	
			this.score5.InitBase();	
			this.score6.InitBase();	

			if(array.length > 0)
			{
				/**
				var tArray:Array = array.slice().sortOn("userScore");				
				
				for(i = 0; i < tArray.length; i++)
				{					
					if(i < 6)
					{
						var pObj:CGamePlayerInfo = tArray[i];
						
						ScoreLines(this["score" + Number(i+1)]).id = Number(i+1);
						ScoreLines(this["score" + Number(i+1)]).info = pObj;
					}
				}
				//**/
				var baseArray:Array = new Array();
				for(i = 0; i < array.length; i++)
				{
					pObj = array[i];
					
					if(!pObj.isDead)
					{
						pObj.addScore = numTime * (GStatic.ADD_SCORE_DEAD + GStatic.nowRoom.gameSpeed);						
					} 
					else
					{
						pObj.addScore = 0;
					}
					
					if(GStatic.nowRoom.boxNumber == BoxEvent.HARD_GAME)
					{
						pObj.addScore *= 3;
					}
					
					pObj.userScore += pObj.addScore;
										
					obj = new Object();
					obj.id = pObj.userID;
					obj.score = pObj.userScore;
					
					baseArray.push(obj);					
				} 
				
				baseArray.sortOn("score", Array.NUMERIC | Array.DESCENDING);				
				
				// trace("nowBaseArray::" + baseArray);
				
				for(i = 0; i < baseArray.length; i++)
				{
					obj = baseArray[i];					
					for(j = 0; j < array.length; j++)
					{
						pObj = array[j];
						if(pObj.userScore == obj.score && pObj.userID.indexOf(obj.id) >= 0)
						{
							if(i < 6)
							{
								// trace("find Lines");
								ScoreLines(this["score" + Number(i+1)]).id = Number(i+1);
								ScoreLines(this["score" + Number(i+1)]).info = pObj;
								if(pObj.userID.indexOf(GStatic.myInfo.userID) >= 0)
								{
									// ScoreLines(this["score" + Number(i+1)]).bold = true;
									ScoreLines(this["score" + Number(i+1)]).color = 0xFFFF00;
								}
								else
								{
									// ScoreLines(this["score" + Number(i+1)]).bold = false;
									ScoreLines(this["score" + Number(i+1)]).color = 0xFFFFFF;
								}
								break;
							}
						}
					}
				} 
				//**/
				Show();		
			}
		}
		
		private function onCloseThis(e:MouseEvent):void
		{
			Hidden();
		}
	}
}